package com.jj.Dialect;

import com.jj.entity.menu;
import com.jj.entity.userlogin;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.context.WebEngineContext;
import org.thymeleaf.model.IModel;
import org.thymeleaf.model.IModelFactory;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractElementTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.templatemode.TemplateMode;

import javax.servlet.http.HttpSession;
import java.util.List;

public class MenuIterator extends AbstractElementTagProcessor {

    private RedisTemplate redisTemplate;
    public MenuIterator(RedisTemplate redisTemplate) {
        super(
                TemplateMode.HTML,
                "bs",
                "m",
                true,
                null,
                false,
                1000);
        this.redisTemplate=redisTemplate;
    }

    @Override
    protected void doProcess(ITemplateContext context,
                             IProcessableElementTag elementTag,
                             IElementTagStructureHandler handler) {
        //获取全量有序的菜单
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        List<menu> menusAll = (List<menu>) redisTemplate.opsForValue().get("menus");
        //获取用户userId
        HttpSession session = ((WebEngineContext) context).getRequest().getSession();
        userlogin user = (userlogin) session.getAttribute("user");
        //获取用户的菜单
        List<menu> umenus = ( List<menu>) redisTemplate.opsForValue().get("se");
        IModelFactory modelFactory = context.getModelFactory();
        IModel model = modelFactory.createModel();

        //遍历第一层
        for (menu m1 : menusAll) {
            if (!umenus.contains(m1))
                continue;

            model.add(modelFactory.createOpenElementTag("ul"));
            model.add(modelFactory.createOpenElementTag("li"));
            model.add(modelFactory.createText(m1.getMenu_name()));
            //遍历第二层菜单
            model.add(modelFactory.createOpenElementTag("ul"));
            for (menu c1 : m1.getMenuList()){
                if (!umenus.contains(c1)){
                    continue;}

                model.add(modelFactory.createOpenElementTag("li"));
                model.add(modelFactory.createText(c1.getMenu_name()));
                model.add(modelFactory.createOpenElementTag("ul"));
                for (menu c2 : c1.getMenuList()){
                    if (!umenus.contains(c2))
                        continue;

                    model.add(modelFactory.createOpenElementTag("li"));
                    model.add(modelFactory.createText(c2.getMenu_name()));
                    model.add(modelFactory.createOpenElementTag("li"));
                }
                model.add(modelFactory.createCloseElementTag("ul"));
                model.add(modelFactory.createCloseElementTag("li"));
            }
            model.add(modelFactory.createCloseElementTag("ul"));
            model.add(modelFactory.createCloseElementTag("li"));
        }
        model.add(modelFactory.createCloseElementTag("ul"));

        handler.replaceWith(model, Boolean.parseBoolean("false"));
    }
}
